﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WCrawlerLib;

namespace WCrawler
{
    public partial class frmSeachForumThreads : System.Web.UI.Page
    {
        private Forum _forum;
        /// <summary>
        /// .//table[@id='threadslist']/tbody/tr
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ForumId"]))
                {

                    _forum = new Forum();
                    _forum = _forum.GetForum(int.Parse(Request.QueryString["ForumId"]));
                    hyperLinkForumUrl.Text = GeneralClass.GetName(_forum.ForumUrl);
                    hyperLinkForumUrl.NavigateUrl = _forum.ForumUrl;
                }
            }
        }

        public DataTable CreateDataTable()
        {
            DataTable myTable;

            // Create a new DataTable.
            myTable = new DataTable("SearchForumThread");
            
            myTable.Columns.Add("Id", System.Type.GetType("System.String"));
            myTable.Columns.Add("Topic", System.Type.GetType("System.String"));
            //myTable.Columns.Add("TitleUrl", System.Type.GetType("System.String"));
            myTable.Columns.Add("Replies", System.Type.GetType("System.String"));
            myTable.Columns.Add("Views", System.Type.GetType("System.String"));
            myTable.Columns.Add("Forum", System.Type.GetType("System.String"));
            //myTable.Columns.Add("ForumUrl", System.Type.GetType("System.String"));

            return myTable;
        }

        public void GetForumSession()
        {
            //Get obj _forum
            if (Session[Request.QueryString["ForumId"]] != null)
            {
                _forum = (Forum)Session[Request.QueryString["ForumId"]];
            }
            else
            {
                _forum = new Forum();
                _forum = _forum.GetForum(int.Parse(Request.QueryString["ForumId"]));
                _forum.InitSearchThreads();
                Session[Request.QueryString["ForumId"]] = _forum;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Session.Remove(Request.QueryString["ForumId"]);
            GetForumSession();
            ForumTemplateDetail obj = new ForumTemplateDetail();
            string statiticUrl = obj.GetStatisticUrl(_forum.ForumId, _forum.ForumTypeId);
            DataTable _myTable;
            if (Session["myTable" + _forum.ForumId.ToString()] == null)
            {
                _myTable = CreateDataTable();
                _myTable = _forum.Browser.SearchAccountThreads(_forum.ForumUrl, statiticUrl, _myTable);
                Session["SearhForumThreadTable" + _forum.ForumId.ToString()] = _myTable;
            }
            else
                _myTable = (DataTable)Session["SearhForumThreadTable" + _forum.ForumId.ToString()];
            
            gridViewSeachForumThread.DataSource = _myTable;
            gridViewSeachForumThread.DataBind();
        }
    }
}